package com.hongning.railway.ui.main.fragments.college;

import android.arch.lifecycle.Observer;
import android.arch.lifecycle.ViewModelProviders;
import android.os.Bundle;
import android.support.annotation.Nullable;
import android.view.LayoutInflater;
import android.view.ViewGroup;

import com.hongning.railway.R;
import com.hongning.railway.app.AppViewModelFactory;
import com.hongning.railway.app.Constant;
import com.hongning.railway.databinding.FragmentLearnCompleteBinding;
import com.hongning.railway.entity.resbean.ReqFilterBean;
import com.hongning.railway.util.Apputils;

import me.goldze.mvvmhabit.BR;
import me.goldze.mvvmhabit.base.BaseFragment;
import me.goldze.mvvmhabit.base.BaseViewModel;
import me.goldze.mvvmhabit.binding.command.BindingConsumer;
import me.goldze.mvvmhabit.bus.Messenger;

public class LearnCompleteFragment extends BaseFragment<FragmentLearnCompleteBinding, LearnCompleteFragmentViewModel>{
    public static LearnCompleteFragment newInstance() {
        
        Bundle args = new Bundle();
        
        LearnCompleteFragment fragment = new LearnCompleteFragment();
        fragment.setArguments(args);
        return fragment;
    }
    @Override
    public int initContentView(LayoutInflater inflater, @Nullable ViewGroup container, @Nullable Bundle savedInstanceState) {
        return R.layout.fragment_learn_complete;
    }

    @Override
    public int initVariableId() {
        return BR.viewModel;
    }


    @Override
    public LearnCompleteFragmentViewModel initViewModel() {
        AppViewModelFactory appViewModelFactory = AppViewModelFactory.getInstance(getActivity().getApplication());
        return ViewModelProviders.of(this,appViewModelFactory).get(LearnCompleteFragmentViewModel.class);
    }

    @Override
    public void initData() {
        super.initData();
        Apputils.setDefaultHeaderView(binding.trRefhreh);
        binding.rvContent.setItemAnimator(null);
        binding.layoutStatus.ivStatu.setImageResource(R.mipmap.icon_nocontent);
        binding.layoutStatus.tvStatu.setText("暂无内容");
        viewModel.getData();
        Messenger.getDefault().register(this,Constant.filter_code, ReqFilterBean.class, new BindingConsumer<ReqFilterBean>() {
            @Override
            public void call(ReqFilterBean reqFilterBean) {
                if (reqFilterBean==null)return;
                viewModel.videoBean.setNewsCategory(reqFilterBean.getNewsCategory());
                viewModel.videoBean.setNewsSubTypeId(reqFilterBean.getNewsSubTypeId());
                viewModel.page=1;
                viewModel.getData();
            }
        });
    }
    public void setloadByKeyord(String keyword){
        viewModel.videoBean.setNewsCategory(null);
        viewModel.videoBean.setNewsSubTypeId(null);
        viewModel.page=1;
        viewModel.videoBean.setNewsTitle(keyword);
        viewModel.getData();
    }
    @Override
    public void initViewObservable() {
        viewModel.uc.finishRefresh.observe(this, new Observer() {
            @Override
            public void onChanged(@Nullable Object o) {
                binding.trRefhreh.finishRefreshing();
                binding.trRefhreh.setEnableLoadmore(true);
//                binding.trRefhreh.setHeaderView();

            }
        });

        viewModel.uc.finishLoadMore.observe(this, new Observer() {
            @Override
            public void onChanged(@Nullable Object o) {
                binding.trRefhreh.finishLoadmore();
            }
        });

        viewModel.uc.finishLoadMoreEnd.observe(this, new Observer() {
            @Override
            public void onChanged(@Nullable Object o) {
                binding.trRefhreh.setEnableLoadmore(false);
            }
        });

    }
}
